/*
 * Created by JFormDesigner on Tue Dec 03 21:50:55 GMT+08:00 2024
 */

package com.hafu.view.GYS;

import com.hafu.model.Gysinfo;
import com.hafu.service.GysinfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author Lxj
 */
public class ManageGYSFrm extends JFrame {
    private GysinfoService gysInfoService = new GysinfoService();
    public ManageGYSFrm() {
        initComponents();
        fillTable();
    }
    private void fillTable() {
        ArrayList<Gysinfo> gysInfoList = gysInfoService.getGysInfoByName(new Gysinfo());
        //获取表格模型
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        for (Gysinfo gysInfo : gysInfoList) {
            Vector<Object> rowData = new Vector<>();
            rowData.add(gysInfo.getId());
            rowData.add(gysInfo.getName());
            rowData.add(gysInfo.getTel());
            model.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 根据名字查询供应商信息事件
        String name = textField1.getText();
        Gysinfo gysInfo = new Gysinfo();
        gysInfo.setName(name);
        ArrayList<Gysinfo> gysInfoList = gysInfoService.getGysInfoByName(gysInfo);
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        if (gysInfoList.size()>0){
            for (Gysinfo gysInfo1 : gysInfoList) {
                Vector<Object> rowData = new Vector<>();
                rowData.add(gysInfo1.getId());
                rowData.add(gysInfo1.getName());
                rowData.add(gysInfo1.getTel());
                //添加一行数据
                model.addRow(rowData);
            }
        }else{
            JOptionPane.showMessageDialog(null,"没有查询到供应商信息");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here 获取选中行
        int row = table1.getSelectedRow();
        //得到行的数据
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row,0);
        String name = (String) dtn.getValueAt(row,1);
        String tel = (String) dtn.getValueAt(row,2);
        textField4.setText(name);
        textField5.setText(tel);

    }

    private void button2(ActionEvent e) {
        // TODO add your code here 修改供应商信息事件
        String name = textField4.getText();
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"供应商名称不能为空");
            return;
        }
        String tel = textField5.getText();
        if (StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"供应商电话不能为空");
            return;
        }
        int row = table1.getSelectedRow();
        //得到行的数据
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row,0);
        Gysinfo gysInfo = new Gysinfo(chooseId,name,tel);
        Boolean aBoolean = gysInfoService.updateGysInfo(gysInfo);
        if (aBoolean){
            JOptionPane.showMessageDialog(null,"修改成功");
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }

    private void button3(ActionEvent e) {
        // TODO add your code here 删除供应商信息事件
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row,0);
        Boolean isDeleteGysInfo = gysInfoService.deleteGysInfo(chooseId);
        if (isDeleteGysInfo){
            JOptionPane.showMessageDialog(null,"删除成功");
            fillTable();
        }else{
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        textField4 = new JTextField();
        textField5 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u4f9b\u5e94\u5546");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u4f9b\u5e94\u5546\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {
            scrollPane1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label1)
                                    .addGap(72, 72, 72)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 154, Short.MAX_VALUE)
                                    .addComponent(button1)))
                            .addGap(49, 49, 49))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(9, 9, 9)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label3)
                                .addComponent(label2))
                            .addGap(89, 89, 89)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(textField5, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField4))
                            .addGap(85, 85, 85)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(button3)
                                .addComponent(button2))
                            .addContainerGap(58, Short.MAX_VALUE))))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(button1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2))
                    .addGap(27, 27, 27)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(button3)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label3)
                            .addComponent(textField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                    .addGap(32, 32, 32))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField4;
    private JTextField textField5;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
